/*******************************************************************************
 * Copyright 2012 Steven Rudenko
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.dev.dream.libs.slidemenu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dev.dream.libs.myslidemenulibs.R;

public class SlideMenuAdapter extends BaseAdapter {

	private static final int VIEW_TYPE_CATEGORY = 0;
	private static final int VIEW_TYPE_SETTINGS = 1;
	
	private static final int VIEW_TYPE_SITES = 2;
	private static final int VIEW_TYPES_COUNT = 3;

	private final LayoutInflater mInflater;

	private String[] mTitles;
	private String[] mType;
	private int[] mIcons;

	public SlideMenuAdapter(Context context, String[] title, String[] type, int[] icon) {
		mInflater = LayoutInflater.from(context);
		mTitles = title;
		mType = type;
		mIcons = icon;
	}

	@Override
	public int getCount() {
		return mType.length;
	}

	@Override
	public String getItem(int position) {
		return mType[position];
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("DefaultLocale")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int type = getItemViewType(position);

		final ViewHolder holder;
		if (convertView == null) {
			if (type == VIEW_TYPE_CATEGORY)
				convertView = mInflater.inflate(R.layout.category_list_item,
						parent, false);
			else
				convertView = mInflater.inflate(R.layout.action_list_item,
						parent, false);

			holder = new ViewHolder();
			holder.text = (TextView) convertView
					.findViewById(android.R.id.text1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (type != VIEW_TYPE_CATEGORY) {
			final Drawable icon = convertView.getContext().getResources()
					.getDrawable(mIcons[position]);
			icon.setBounds(0, 0, icon.getIntrinsicWidth(),
					icon.getIntrinsicHeight());
			holder.text.setCompoundDrawables(icon, null, null, null);
			holder.text.setText(mTitles[position]);
		} else {
			holder.text.setText(mTitles[position].toUpperCase());
		}

		return convertView;
	}

	@Override
	public int getViewTypeCount() {
		return VIEW_TYPES_COUNT;
	}

	@Override
	public int getItemViewType(int position) {
		
		if (mType[position].equals("0"))
			return VIEW_TYPE_CATEGORY;
		else if (mType[position].equals("1"))
			return VIEW_TYPE_SETTINGS;
		return VIEW_TYPE_SITES;
	}

	@Override
	public boolean isEnabled(int position) {
		return getItemViewType(position) != VIEW_TYPE_CATEGORY;
	}

	private static class ViewHolder {
		TextView text;
	}
}
